/**
 * 
 */
package com.cgbsoft.financial.dao;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import com.cgbsoft.financial.domain.MyTransaction;

/**
 * @author CX
 *
 */
public class CacheTransactionDB {
	
	/**
	 * 保存本机课程，如果没有，则创建一条
	 * @param context
	 * @param MyTransaction
	 * @return
	 */
	public static long saveOrUpdateMyTransaction(Context context, MyTransaction transaction) {
		long id = -1;
		
		MyTransaction MyTransactionOld = findMyTransactionBySubject(context, transaction.getsSubject());
		if (MyTransactionOld != null) {
			transaction.setId(MyTransactionOld.getId());
			transaction.setmCount(MyTransactionOld.getmCount() + 1);
			id = updateMyTransaction(context, transaction);
		} else {
			id = insertMyTransaction(context, transaction);
		}
		
		return id;
	}
	
	/**
	 * 添加课程
	 * @param context
	 * @param MyTransaction
	 * @param cardType
	 * @return
	 */
	public static long insertMyTransaction(Context context, MyTransaction MyTransaction) {
		long id = -1;
		try {
			DataBaseHelper dbHelper = new DataBaseHelper(context);
			id = dbHelper.getReadableDatabase().insert(DataBaseHelper.T_TRANSTION_CACHE,
					"_id", transactionToContentValues(MyTransaction));
			dbHelper.getReadableDatabase().close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return id;
	}
	
	/**
	 * 更新课程
	 * @param context
	 * @param MyTransaction
	 * @param cardType
	 * @return
	 */
	public static long updateMyTransaction(Context context, MyTransaction MyTransaction) {
		long id = -1;
		try {
			DataBaseHelper dbHelper = new DataBaseHelper(context);
			id = dbHelper.getReadableDatabase().update(DataBaseHelper.T_TRANSTION_CACHE,
					transactionToContentValues(MyTransaction), 
					" _id=? ", 
					new String[] {String.valueOf(MyTransaction.getId())});
			dbHelper.getReadableDatabase().close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return id;
	}
	
	/**
	 * 查找课程
	 * @param context
	 * @return
	 */
	public static List<MyTransaction> findTransactions(Context context, String selection, String[] selectionArgs) {
		DataBaseHelper dbHelper = new DataBaseHelper(context);
		
		String[] columns = new String[] { "_id", "DATE", "SUM", "SUBJECT", "CATEGORY", "M_COUNT" };
		
		List<MyTransaction> MyTransactionList = new ArrayList<MyTransaction> ();
		
		try {
			Cursor cursor = dbHelper.getReadableDatabase().query(DataBaseHelper.T_TRANSTION_CACHE, columns, selection, selectionArgs, null, null, " M_COUNT desc");
			if (cursor.getCount() > 0) {
				cursor.moveToFirst();
				MyTransaction myTransaction;
				for (int i = 0; i < cursor.getCount(); i++) {
					myTransaction = new MyTransaction();
					myTransaction.setId(cursor.getInt(0));
					myTransaction.setdDate(cursor.getString(1));
					myTransaction.setiSum(cursor.getString(2));
					myTransaction.setsSubject(cursor.getString(3));
					myTransaction.setcCatagory(cursor.getString(4));
					myTransaction.setmCount(cursor.getInt(5));
					
					MyTransactionList.add(myTransaction);
					cursor.moveToNext();
					if (i > 4) break;//只找最多的前5条 
				}
			}
			cursor.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return MyTransactionList;
	}
	
	/**
	 * 根据ID查找记录
	 * @param context
	 * @param id
	 * @return
	 */
	public static MyTransaction findMyTransactionById(Context context, String id) {
		MyTransaction MyTransaction = null;
		
		List<MyTransaction> MyTransactionList = findTransactions(context, " _id=? ", new String[] {id});
		if (MyTransactionList.size() > 0) {
			MyTransaction = MyTransactionList.get(0);
		}
		
		return MyTransaction;
	}
	
	/**
	 * 根据主题查找帐单记录
	 * @param context
	 * @param transactionNum
	 * @param weekDayNum
	 * @return
	 */
	public static MyTransaction findMyTransactionBySubject(Context context, String subject) {
		MyTransaction MyTransaction = null;
		
		List<MyTransaction> MyTransactionList = findTransactions(context, " SUBJECT = ? ", new String[] {subject});
		if (MyTransactionList.size() > 0) {
			MyTransaction = MyTransactionList.get(0);
		}
		
		return MyTransaction;
	}
	
	/**
	 * 根据主题字查找查找记录
	 * @param context
	 * @param id
	 * @return
	 */
	public static List<MyTransaction> findMyTransactionsBySubect(Context context, String subject) {

		List<MyTransaction> MyTransactionList = findTransactions(context, " SUBJECT like '%"+subject+"%' ", null);
		
		return MyTransactionList;
	}
	
	
	/**
	 * @param transaction
	 * @return
	 */
	private static ContentValues transactionToContentValues(MyTransaction transaction) {
		ContentValues contentValues = new ContentValues();
		contentValues.put("DATE", transaction.getdDate());
		contentValues.put("SUM", transaction.getiSum());
		contentValues.put("SUBJECT", transaction.getsSubject());
		contentValues.put("CATEGORY", transaction.getcCatagory());
		contentValues.put("M_COUNT", transaction.getmCount());
		return contentValues;
	}
	
	
}
